package z_guestbook;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.PreparedStatement;

public class Input extends JInternalFrame {
	GuestBookMain main;
	
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JTextField mid;
	private JTextField subject;
	private JScrollPane scrollPane;
	private JTextPane content;
	private JButton btnNewButton;


	public Input(GuestBookMain main) {
		super("방명록 입력", true, true, true, true);
		setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
		this.main = main;
		
		setBounds(100, 100, 542, 357);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getLblNewLabel_2());
		getContentPane().add(getMid());
		getContentPane().add(getSubject());
		getContentPane().add(getScrollPane());
		getContentPane().add(getBtnNewButton());

	}
	
	public void insert(){
		String sql = "insert into guestbook(serial, mid, subject, content) " +
				"values(gb_serial.nextval, ?,?,?)";
		try{
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setString(1, mid.getText());
			pstmt.setString(2, subject.getText());
			pstmt.setString(3, content.getText());
			pstmt.executeUpdate();
			mid.setText("");
			subject.setText("");
			content.setText("");
			mid.requestFocus();
			if(main.select != null) main.select.setData();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("아이디");
			lblNewLabel.setBounds(12, 10, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("제목");
			lblNewLabel_1.setBounds(12, 35, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("내용");
			lblNewLabel_2.setBounds(12, 60, 57, 15);
		}
		return lblNewLabel_2;
	}
	private JTextField getMid() {
		if (mid == null) {
			mid = new JTextField();
			mid.setBounds(62, 7, 116, 21);
			mid.setColumns(10);
		}
		return mid;
	}
	private JTextField getSubject() {
		if (subject == null) {
			subject = new JTextField();
			subject.setBounds(62, 35, 452, 21);
			subject.setColumns(10);
		}
		return subject;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 85, 502, 201);
			scrollPane.setViewportView(getContent());
		}
		return scrollPane;
	}
	private JTextPane getContent() {
		if (content == null) {
			content = new JTextPane();
		}
		return content;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("저장");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					insert();
				}
			});
			btnNewButton.setBounds(222, 296, 97, 23);
		}
		return btnNewButton;
	}
}
